#include <stdio.h>
#include <stdlib.h>
#include "net_file_decompress.h"

// 从文件中加载数据
static char* preload(const char* path, size_t* data_size)
{
    FILE* file = fopen(path, "rb");
    char* data;
    *data_size = 0;
    if (!file)
        return 0;
    if (fseek(file, 0, SEEK_END))
        exit(1);
    *data_size = (size_t)ftell(file);
    if (*data_size < 0)
        exit(1);
    if (fseek(file, 0, SEEK_SET))
        exit(1);
    data = (char*)malloc(*data_size);
    if (!data)
        exit(1);
    if ((size_t)fread(data, 1, *data_size, file) != *data_size)
        exit(1);
    fclose(file);
    return data;
}

int test_gzip() {
    // 模拟 gzip 数据，这里你应该使用实际的 gzip 数据
    char* gzip_data;
    size_t gzip_data_size;
    gzip_data = preload("..\\test_files\\test_01.gzip", &gzip_data_size);
    if (gzip_data == NULL) {
        fprintf(stderr, "Failed to load gzip data\n");
        return 1;
    }

    // 解压后缓冲区大小，初始估计
    size_t decompressed_size = 1024 * 1024;  // 1MB，实际大小视情况而定
    size_t actual_decompressed_size = 0;     // 用于存储实际解压后的数据大小

    unsigned char* decompressed_data = malloc(decompressed_size);
    if (decompressed_data == NULL) {
        fprintf(stderr, "Failed to allocate memory for decompressed data\n");
        return 1;
    }

    // 调用解压函数
    int result = decompress_gzip(gzip_data, gzip_data_size, decompressed_data, decompressed_size, &actual_decompressed_size);
    if (result == Z_OK) {
        printf("Decompression succeeded, actual decompressed size is: %zu\n", actual_decompressed_size);
        // 处理解压后的数据
    } else {
        printf("Decompression failed with error code: %d\n", result);
    }

    // 释放内存
    free(decompressed_data);
    free(gzip_data);
    return 0;
}

